//
//  ApprovalSuggestionViewController.swift
//  HuaYun
//
//  Created by enghou on 2018/7/13.
//  Copyright © 2018年 xyxorigation. All rights reserved.
//

import UIKit

class ApprovalSuggestionViewController: BaseViewController {

    private var _netPath = PurchasePayApproval
    var netPath: String {
        get{
            return _netPath
        }
        set{
            _netPath = newValue
        }
    }
    
    var applyId = ""
    
    var businessType = ""
    
    var ifpost = false
    
    @IBOutlet weak var suggestionTextView: XXTextView!
    
    @IBAction func rejectAction(_ sender: UIButton) {
        self.load(with: "reject")
    }
    
    @IBAction func agreeAction(_ sender: UIButton) {
        self.load(with: "agree")
    }
    
    override var names: Array<String> {
        get{
            return ["reject","agree"]
        }
    }
    
    override func success(for name: String) -> NetSuccess{
        
        func suc(json: JSON)->Void{
            if let success = json.contents?["success"]?.rawValue as? Bool {
                if success == true {
                    self.dismiss(animated: false) {
                        NotificationCenter.default.post(name: NSNotification.Name("RefreshDetail"), object: nil)
                    }
                } else {
                    self.alert(json.contents?["errorMsg"]?.rawString())
                }
            }
        }
        
        return suc
    }
    
    override func param(for name: String) -> Dictionary<String,Any>{
        if name == "agree" {
            return ["applyId":applyId,"businessType":businessType,"remark":self.suggestionTextView.text.trimmingCharacters(in: CharacterSet.illegalCharacters),"approvalStatus":0]
        }else{
            if self.netPath == ReimbursementApproval {
                return ["applyId":applyId,"businessType":businessType,"remark":self.suggestionTextView.text.trimmingCharacters(in: CharacterSet.illegalCharacters),"approvalStatus":1]
            }
            return ["applyId":applyId,"businessType":businessType,"remark":self.suggestionTextView.text.trimmingCharacters(in: CharacterSet.illegalCharacters),"approvalStatus":100]
        }
    }
    
    override func path(for name: String) -> String{
        
        return netPath
    }
    
    override func method(for name: String) -> NetMethod{
        if ifpost == true {
            return .post
        } else {
            return .get
        }
    }
    
    //响应接口的有效性策略,如果参数不合格，则默认根据String提示
    override func strategy(for name: String) -> (Bool,String?,(()->Void)?){
        if name == "reject" {
            let realText = self.suggestionTextView.text.trimmingCharacters(in: CharacterSet.illegalCharacters)
            
            if realText.count == 0 {
                return (false,"审批意见不能为空",nil)
            }
        }
        return (true,nil,nil)
    }
    
    @IBOutlet weak var trayView: UIView!
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        let height = self.view.width * 329 / 375
        let animation = POPSpringAnimation.init(propertyNamed: kPOPViewFrame)
        animation?.beginTime = CACurrentMediaTime()
        animation?.toValue = NSValue.init(cgRect: CGRect.init(origin: CGPoint.init(x: 0, y: self.view.height - height), size: CGSize(width: self.view.width, height: height)))
        animation?.springSpeed = 20
        animation?.springBounciness = 5
        self.trayView.pop_add(animation, forKey: "t")
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        
        self.view.endEditing(true)
        
        self.dismiss()
        
    }

}
